home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / VOL_400 / 466_01 / SRC / FMTTOKEN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-20  |  5.7 KB  |  292 lines

  1. #include <afx.h>
  2. #include <afxtempl.h>
  3. #include "parse.h"
  4. #include "input.h"
  5. #include "fmt.h"
  6. #include "fmtToken.h"
  7. #include "errmsg.h"
  8.  
  9.  
  10.  
  11.  
  12. /******************************************************/
  13. // Tag list
  14. /******************************************************/
  15.  
  16. TAGSPEC gtagsTokenSection[] =
  17. {
  18.     CFmtListToken::tagOutput,         "output",
  19.     CFmtListToken::tagExtension,    "extension",
  20.     CFmtListToken::tagToken,        "token",
  21.     CFmtListToken::tagHighCharMask, "highcharmask",
  22.     CFmtListToken::tagContext,        "context",
  23.     -1,        NULL,
  24. };
  25.  
  26. TAGSPEC gtagsTokenContexts[] =
  27. {
  28.     ctxBody, "body",
  29.     ctxExample, "example",
  30.     -1, NULL,
  31. };
  32.  
  33. const int gnTokens = 0;
  34.  
  35.  
  36. /******************************************************/
  37. // Format class
  38. /******************************************************/
  39.  
  40. CFmtToken::CFmtToken(void) : CFmtBase(gnTokens)
  41. {
  42.     m_ptok = NULL;
  43. }
  44.  
  45.  
  46.  
  47. CFmtToken::~CFmtToken(void)
  48. {
  49.     TOKEN *ptok, *ptokNext;
  50.  
  51.     for(ptok = m_ptok; ptok; ptok = ptokNext)
  52.     {
  53.         ptokNext = ptok->next;
  54.         delete ptok;
  55.     }
  56. }
  57.  
  58.  
  59. int CFmtToken::Validate(void)
  60. {
  61.     TOKEN *ptok;
  62.  
  63.     for(ptok = m_ptok; ptok; ptok = ptok->next)
  64.         m_sControlChars += ptok->chToken;
  65.  
  66.     return 0;
  67. }
  68.  
  69.  
  70. CString &CFmtToken::GetToken(char ch, TokenContexts context)
  71. {
  72.     extern CString sEmpty;
  73.  
  74.     TOKEN *ptok;
  75.  
  76.     // First, search the context list
  77.     
  78.     for(ptok = m_ptok; ptok; ptok = ptok->next)
  79.         if(ptok->chToken == ch && (ptok->context == context))
  80.             return ptok->sOutput;
  81.  
  82.     // If we searched the tokens list and didn't come up with
  83.     // one for a special context, search again using the body
  84.     // context. If that one turns up nothing, there is something
  85.     // weird here...
  86.  
  87.     if(ptok == NULL && context != ctxBody)
  88.     {
  89.         for(ptok = m_ptok; ptok; ptok = ptok->next)
  90.             if(ptok->chToken == ch && (ptok->context == ctxBody))
  91.                 return ptok->sOutput;
  92.     }
  93.  
  94.     // this should not happen, toss out a warning message...
  95.     PrintError(NULL, NO_LINE, warnTokenContextMismatch);
  96.  
  97.     return sEmpty;
  98. }
  99.  
  100.  
  101. #ifdef _DEBUG
  102. void CFmtToken::Dump(CDumpContext &dc) const
  103. {
  104.     CFmtBase::Dump(dc);
  105.  
  106.     dc << "\tControl:\t\t\t[" << m_sControlChars << "]\n"
  107.        << "\tHigh char mask:\t\t[" << m_sHighCharMask << "]\n\n";
  108. }
  109. #endif
  110.  
  111.  
  112.  
  113. /******************************************************/
  114. // List class
  115. /******************************************************/
  116.  
  117. TAGSPEC *CFmtListToken::FmtTagList(void)
  118. {
  119.     return gtagsTokenSection;
  120. }
  121.  
  122. int CFmtListToken::ParseEntry(CFmtInput &in)
  123. {
  124.     CFmtToken *pNew;
  125.     const char *sz;
  126.     static char szNameTerm[] = "\t ";
  127.     int nRet;
  128.     const char *szEnd;
  129.     TOKEN *ptokNext;
  130.  
  131.     pNew = (CFmtToken *)m_pNew;
  132.         
  133.     switch(m_nTag)
  134.     {
  135.     // Parent
  136.     case tagOutput:
  137.         
  138.         // Expect: .output = output, extension
  139.  
  140.         if(in.m_nTokens != 2)
  141.             return fmterrBadEntryCount;
  142.  
  143.         if(!CheckOutputType(in, 0))
  144.             return 0;
  145.  
  146.         sz = EatWhite(in.m_aszTokens[1]);
  147.         szEnd = SeekEnd(sz, szNameTerm, MAXTAGSIZE);
  148.         if(szEnd == NULL)
  149.             return fmterrBadFilenameExtension;
  150.  
  151.         // Make a new one
  152.  
  153.         nRet = CheckAddTag();
  154.         if(nRet)
  155.             return nRet;
  156.  
  157.         m_pNew = pNew = new CFmtToken;
  158.  
  159.         m_nState.Tag = TRUE;
  160.  
  161.         // Record source file information.
  162.  
  163.         pNew->SetSource(in.m_nFile, in.m_lCurLine);
  164.         pNew->m_sExtension = CString(sz, szEnd-sz);
  165.  
  166.         break;
  167.  
  168.     case tagContext:
  169.     {
  170.         static char szNameTerm[] = "\t ";
  171.         const char *sz1;
  172.         const char *sz2;
  173.         int i;
  174.  
  175.         if(m_nState.Skip)
  176.             return 0;
  177.  
  178.            // expect: .context=body | example
  179.  
  180.         if(in.m_nTokens != 1)
  181.             return fmterrBadEntryCount;
  182.  
  183.         sz1 = EatWhite(in.m_aszTokens[0]);
  184.         sz2 = SeekEnd(sz1, szNameTerm, MAXTAGSIZE);
  185.         if(sz2 == NULL)
  186.             return fmterrBadTokenContext;
  187.  
  188.         CString sName(sz1, sz2-sz1);
  189.  
  190.         // Get the source parsing type name
  191.     
  192.         for(i = 0; gtagsTokenContexts[i].iTag != -1; i++)
  193.         {
  194.             if(_stricmp(sName, gtagsTokenContexts[i].szName) == 0)
  195.                 break;
  196.         }
  197.                 
  198.            if(gtagsTokenContexts[i].iTag == -1)
  199.                return fmterrBadTokenContext;
  200.  
  201.         m_context = gtagsTokenContexts[i].iTag;
  202.  
  203.         return 0;
  204.     }
  205.  
  206.  
  207.     case tagToken:
  208.         if(m_nState.Skip)
  209.             return 0;
  210.  
  211.         if(!m_nState.Tag)
  212.             return fmterrOrphanedTag;
  213.  
  214.            // expect: .token=c, text
  215.  
  216.         if(in.m_nTokens != 2)
  217.             return fmterrBadEntryCount;
  218.  
  219.         sz = EatWhite(in.m_aszTokens[0]);
  220.         char ch;
  221.  
  222.         if(*sz == chCaret)
  223.         {
  224.             switch(sz[1])
  225.             {
  226.             case 'p': case 'P':
  227.                 ch = chNewline;
  228.                 break;
  229.  
  230.             case 't': case 'T':
  231.                 ch = chTab;
  232.                 break;
  233.  
  234.             default:
  235.                 return fmterrBadTokenCode;
  236.             }
  237.             sz++;
  238.         }
  239.         else
  240.         {
  241.             ch = *sz;
  242.         }
  243.         sz = EatWhite(++sz);
  244.         if(*sz)
  245.             return fmterrBadTokenCode;
  246.  
  247.         ptokNext = pNew->m_ptok ? pNew->m_ptok : NULL;
  248.  
  249.         pNew->m_ptok = new TOKEN;
  250.         pNew->m_ptok->next = ptokNext;
  251.         pNew->m_ptok->context = m_context;
  252.         pNew->m_ptok->chToken = ch;
  253.         pNew->m_ptok->sOutput = in.m_aszTokens[1];
  254.         
  255.         break;
  256.  
  257.     case tagHighCharMask:
  258.        
  259.         if(m_nState.Skip)
  260.             return 0;
  261.  
  262.         if(!m_nState.Tag)
  263.             return fmterrOrphanedTag;
  264.  
  265.            // expect: .highcharmask=text
  266.  
  267.         if(in.m_nTokens != 1)
  268.             return fmterrBadEntryCount;
  269.  
  270.         // Assign mask.
  271.  
  272.         pNew->m_sHighCharMask = EatWhite(in.m_aszTokens[0]);
  273.  
  274.         break;
  275.  
  276.     default:
  277.         return fmterrBadFmtEntry;
  278.     }
  279.  
  280.     return 0;
  281. }
  282.  
  283.  
  284.  
  285. int CFmtListToken::Validate(void)
  286. {
  287.     if(m_listTags.GetCount() == 0)
  288.         return fmterrNoTokenDefinitions;
  289.  
  290.     return 0;
  291. }
  292.